package com.zzx.threekingdom.judge;

import com.zzx.threekingdom.card.BaseCard;
import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.judge.Judge.IPassAction;
import com.zzx.threekingdom.judge.Judge.IPassCard;
import com.zzx.threekingdom.model.AbsPlayerModel;
import com.zzx.threekingdom.model.ActionInfo;

public class AskPeach extends AskAll
{
    public void setDyingPm(AbsPlayerModel pm) { actionInfo.to = pm; }
    public AbsPlayerModel getDyingPm() { return actionInfo.to; }
    public boolean isValidAskInfo() { return (actionInfo.to!=null&&actionInfo.to.health<=0); }
    
    public void beginAsk(AbsPlayerModel pmDying, Runnable doneR)
    {
        actionInfo.clear();
        actionInfo.setActionName("求桃");
        setDyingPm(pmDying);
        setDone(doneR);
        if(isValidAskInfo()==false)
            endAsk();
        else
        {
            Judge.judge.addLog(pmDying.getCharacterName() + "濒死求桃");
            beginAsk(Judge.judge.getLivePmFromeCurrentTurn(), doneR);
        }
    }
    
    public void doAsk(final AbsPlayerModel pm, Runnable doneR)
    {
        IPassAction ipa = new IPassAction() { public void onGotAction(ActionInfo act) {
			if(act != null && act.getActionName().equals("桃"))
			{
				pm.drawCard(act.getCard());
                Judge.judge.addLog(pm.getCharacterName() + "出桃");
                getDyingPm().setHealth(getDyingPm().health + 1);
                if(getDyingPm().health>0)
                {
                    endAsk();
                    return;
                }
                askCurrent();
			}
			else
                goOnAsk();
		}};
        Judge.judge.addLog(getDyingPm().getCharacterName() + "向" + pm.getCharacterName() + "求桃");
        actionInfo.from = pm;
        pm.onNeedActionByName("桃", ipa, actionInfo);
        
    }
    @Override
    public void endAsk()
    {
        if(isValidAskInfo()==true)
        {
            if(getDyingPm().health<=0)
                getDyingPm().setDead();
        }
        
        actionInfo.clear(); 
        if(Judge.judge.isGameEnd()==false)
        {
            super.endAsk();
        }
        
    }
}
